package com.goffity.pcp;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class PoliceCheckPointActivity extends MapActivity {

	private boolean gpsStarted;
	private LocationManager mlocManager;
	private final String LOG_TAG = "pcp";
	private Location myLocation;
	private LocationListener mlocListener;
	protected String currentLocation;
	private MapView mapView;
	private MapController mapController;
	private MyLocationOverlay myLocationOverlay;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mapView = (MapView) findViewById(R.id.mapview);

		initMap();

		mapView.setBuiltInZoomControls(true);
		// mapView.setStreetView(true);
		mapController = mapView.getController();
		mapController.setZoom(14);

		if (!gpsStarted) {
			new AlertDialog.Builder(this)
					.setTitle("Enable GPS?")
					.setMessage(R.string.confirm_gps)
					.setNeutralButton(R.string.no,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
								}
							})
					.setPositiveButton(R.string.yes,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									start_gps();
								}
							}).show();
		}

		mlocListener = new LocationListener() {

			public void onStatusChanged(String provider, int status,
					Bundle extras) {
				// TODO Auto-generated method stub

			}

			public void onProviderEnabled(String provider) {
				Toast.makeText(getApplicationContext(), "Gps Enabled",
						Toast.LENGTH_SHORT).show();
			}

			public void onProviderDisabled(String provider) {
				Toast.makeText(getApplicationContext(), "Gps Disabled",
						Toast.LENGTH_SHORT).show();
			}

			public void onLocationChanged(Location location) {
				currentLocation = "My current location is: \n" + "Latitud = "
						+ location.getLatitude() + "\n" + "Longitud = "
						+ location.getLongitude();

				GeoPoint point = new GeoPoint(
						(int) (location.getLatitude() * 1E6),
						(int) (location.getLongitude() * 1E6));
				mapController.animateTo(point);

				Toast.makeText(getApplicationContext(), currentLocation,
						Toast.LENGTH_SHORT).show();
			}
		};
	}

	private void initMap() {
		myLocationOverlay = new MyLocationOverlay(getApplicationContext(),
				mapView);
		mapView.getOverlays().add(myLocationOverlay);
		myLocationOverlay.enableCompass();
		myLocationOverlay.enableMyLocation();
		myLocationOverlay.runOnFirstFix(new Runnable() {
			public void run() {
				Log.d(LOG_TAG, "" + myLocationOverlay.getMyLocation());
				mapController.animateTo(myLocationOverlay.getMyLocation());
			}
		});
	}

	public boolean start_gps() {
		boolean started = false;
		try {

			mlocManager = mlocManager == null ? (LocationManager) getSystemService(LOCATION_SERVICE)
					: mlocManager;

			if (mlocManager != null) {

				mlocManager.requestLocationUpdates(
						LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

				String lp = mlocManager.getBestProvider(getCriteria(), true);

				if (lp != null && !(lp.equals(""))) {
					myLocation = mlocManager.getLastKnownLocation(lp);

					mlocListener.onLocationChanged(myLocation);

					gpsStarted = true;
					started = true;
				} else {
					gpsStarted = false;
					started = false;
				}
			}
		} catch (Exception e) {
			Toast.makeText(PoliceCheckPointActivity.this, e.getMessage(),
					Toast.LENGTH_LONG).show();
		}
		return started;
	}

	public Criteria getCriteria() {
		Criteria criteria = new Criteria();

		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(true);
		criteria.setBearingAccuracy(Criteria.ACCURACY_FINE);
		criteria.setBearingRequired(true);
		criteria.setSpeedAccuracy(Criteria.ACCURACY_FINE);
		criteria.setSpeedRequired(true);

		return criteria;
	}

	public boolean stop_gps() {
		boolean stopped = false;
		try {
			if (gpsStarted) {
				if (mlocManager != null) {
					mlocManager.removeUpdates(mlocListener);
					mlocManager = null;
				}
				gpsStarted = false;
				Toast.makeText(getApplicationContext(), "GPS disabled",
						Toast.LENGTH_LONG).show();
				stopped = true;
			}
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), e.getMessage(),
					Toast.LENGTH_LONG).show();
		}
		return stopped;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// throw new UnsupportedOperationException("Not supported yet.");
		return false;
	}
}
